/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package schedulerManagerment.Scheduler;

import Application.MarvellFrame;
import DataServices.LoadingBean;
import DataServices.TransactBean;
import connection.Configuration;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTable;
import schedulerManagerment.company.CompanyManager;

/**
 *
 * @author DOANTUNGANH
 */
public class Accounting extends javax.swing.JFrame implements MarvellFrame {

    LoadingBean loading = new LoadingBean();
    TransactBean transact = new TransactBean();
    // Connection conn to connect database
    public static Connection conn = null;
    private int selectedCompanyID;
//    Query used in table List of company
    public String listOfCompany_SQL = "SELECT * FROM COMPANY";

    /**
     * Creates new form Accounting
     */
    public Accounting() {
        initComponents();
        loading.loadTable(listOfCompany_SQL, companyTable);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        companyTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        moneyLbl = new javax.swing.JLabel();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        companyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        companyTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                companyTableMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(companyTable);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("List of Company ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("The fee the selected company must pay : ");

        moneyLbl.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        moneyLbl.setForeground(new java.awt.Color(0, 0, 255));
        moneyLbl.setText("                  ");

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Import Document.png"))); // NOI18N
        btnBack.setText("back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(moneyLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnBack))
                        .addGap(0, 783, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(moneyLbl))
                .addGap(27, 27, 27)
                .addComponent(btnBack)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void companyTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_companyTableMousePressed
        // TODO add your handling code here:
        int row = this.companyTable.getSelectedRow();
        selectedCompanyID = (int) getCellObject(companyTable, row, 0);
        float test = getPaymentOfCompany(selectedCompanyID);
        System.out.println("test : " + test);
        moneyLbl.setText("" + test + " USD");
    }//GEN-LAST:event_companyTableMousePressed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        // TODO add your handling code here:
        this.exitFrame();
    }//GEN-LAST:event_btnBackActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JTable companyTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel moneyLbl;
    // End of variables declaration//GEN-END:variables
    private Object getCellObject(JTable table, int row, int column) {
        return table.getModel().getValueAt(row, column);
    }

    @Override
    public void startFrame(JFrame app) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CompanyManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        this.root = app;
        this.setVisible(true);
        root.setEnabled(false);
    }

    @Override
    public void exitFrame() {
        root.setEnabled(true);
        this.setVisible(false);
    }
    private JFrame root;

    // Config Connection
    private void configConnection() {
        Configuration createConn = new Configuration();
        conn = createConn.getConnect();
    }

    private float getPaymentOfCompany(int companyID) {
        TransactBean transact = new TransactBean();
        ResultSet rs = null;
//        show the sum of salary and counting successful interview of that company
        String sql = "SELECT SUM(SALARY) AS TOTAL, COUNT(*) AS COUNTING FROM INTERVIEW I INNER JOIN VACANCY V ON I.VACANCYID = V.ID WHERE I.RESULT = 1 AND V.COMPANYID = " + companyID;
        float total = 0;
        int counting = 0;
        float payment = 0;
        try {
            rs = transact.getpreparedStatement(sql).executeQuery();
//            assign out of date vacancy ID to outOfDateVacancyID vector array
            while (rs.next()) {
                total = rs.getFloat("TOTAL");
                counting = rs.getInt("COUNTING");
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoadingBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            transact.destroy();
        }
        System.out.println("total money  : " + total);
        if (counting <= 30) {
            payment = (float) (total * 0.05);
        } else if (counting <= 100) {
            payment = (float) (total * 0.08);
        } else if (counting <= 250) {
            payment = (float) (total * 0.1);
        } else if (counting <= 500) {
            payment = (float) (total * 0.15);
        } else if (counting > 500) {
            payment = (float) (total * 0.2);
        }
        
        return payment;
    }
}
